package org.example;

import org.example.entity.Connection;
import org.example.entity.Statement;
import org.example.factory.DBFactory;

import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        DBFactory dbFactory = getDBFactory();
        Connection connection = dbFactory.createConnection();
        Statement statement = dbFactory.createStatement();

    }

    static DBFactory getDBFactory(){
        try {
            //1.get connection name
            String filePath = "D:\\Java\\project\\solfware-designing\\AbstractFactoryMethod\\src\\main\\resources\\db.properties";
            Properties properties = new Properties();
            properties.load(new FileReader(filePath));
            String db = properties.getProperty("db");
            System.out.println(db);
            String dbFactory = "org.example.factory."+db+"Factory";

            //2.get factory bean
            Class c = Class.forName(dbFactory);
            Constructor constructor = c.getConstructor(null);
            return (DBFactory) constructor.newInstance();

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}